home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 023 / ver30 / sys / sysv / fileio.c next >
C/C++ Source or Header  |  1995-03-17  |  4KB  |  175 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *         System V file I/O, differs from ultrix 
  4.  *          only in lack of rename() system call.
  5.  * Version:    0
  6.  * Last edit:    17-Apr-86
  7.  * By:        gonzo!daveb
  8.  *        {sun, amdahl, mtxinu}!rtech!gonzo!daveb
  9.  */
  10. #include    "def.h"
  11.  
  12. static    FILE    *ffp;
  13.  
  14. /*
  15.  * Open a file for reading.
  16.  */
  17. ffropen(fn)
  18. char    *fn;
  19. {
  20.     if ((ffp=fopen(fn, "r")) == NULL)
  21.         return (FIOFNF);
  22.     return (FIOSUC);
  23. }
  24.  
  25. /*
  26.  * Open a file for writing.
  27.  * Return TRUE if all is well, and
  28.  * FALSE on error (cannot create).
  29.  */
  30. ffwopen(fn)
  31. char    *fn;
  32. {
  33.     if ((ffp=fopen(fn, "w")) == NULL) {
  34.         eprintf("Cannot open file for writing");
  35.         return (FIOERR);
  36.     }
  37.     return (FIOSUC);
  38. }
  39.  
  40. /*
  41.  * Close a file.
  42.  * Should look at the status.
  43.  */
  44. ffclose()
  45. {
  46.     fclose(ffp);
  47.     return (FIOSUC);
  48. }
  49.  
  50. /*
  51.  * Write a line to the already
  52.  * opened file. The "buf" points to the
  53.  * buffer, and the "nbuf" is its length, less
  54.  * the free newline. Return the status.
  55.  * Check only at the newline.
  56.  */
  57. ffputline(buf, nbuf)
  58. register char    buf[];
  59. {
  60.     register int    i;
  61.  
  62.     for (i=0; i<nbuf; ++i)
  63.         putc(buf[i]&0xFF, ffp);
  64.     putc('\n', ffp);
  65.     if (ferror(ffp) != FALSE) {
  66.         eprintf("Write I/O error");
  67.         return (FIOERR);
  68.     }
  69.     return (FIOSUC);
  70. }
  71.  
  72. /*
  73.  * Read a line from a file, and store the bytes
  74.  * in the supplied buffer. Stop on end of file or end of
  75.  * line. Don't get upset by files that don't have an end of
  76.  * line on the last line; this seem to be common on CP/M-86 and
  77.  * MS-DOS (the suspected culprit is VAX/VMS kermit, but this
  78.  * has not been confirmed. If this is sufficiently researched
  79.  * it may be possible to pull this kludge). Delete any CR
  80.  * followed by an LF. This is mainly for runoff documents,
  81.  * both on VMS and on Ultrix (they get copied over from
  82.  * VMS systems with DECnet).
  83.  */
  84. ffgetline(buf, nbuf)
  85. register char    buf[];
  86. {
  87.     register int    c;
  88.     register int    i;
  89.  
  90.     i = 0;
  91.     for (;;) {
  92.         c = getc(ffp);
  93.         if (c == '\r') {        /* Delete any non-stray    */
  94.             c = getc(ffp);        /* carriage returns.    */
  95.             if (c != '\n') {
  96.                 if (i >= nbuf-1) {
  97.                     eprintf("File has long line");
  98.                     return (FIOERR);
  99.                 }
  100.                 buf[i++] = '\r';
  101.             }
  102.         }
  103.         if (c==EOF || c=='\n')        /* End of line.        */
  104.             break;
  105.         if (i >= nbuf-1) {
  106.             eprintf("File has long line");
  107.             return (FIOERR);
  108.         }
  109.         buf[i++] = c;
  110.     }
  111.     if (c == EOF) {                /* End of file.        */
  112.         if (ferror(ffp) != FALSE) {
  113.             eprintf("File read error");
  114.             return (FIOERR);
  115.         }
  116.         if (i == 0)            /* Don't get upset if    */
  117.             return (FIOEOF);    /* no newline at EOF.    */
  118.     }
  119.     buf[i] = 0;
  120.     return (FIOSUC);
  121. }
  122.  
  123. /*
  124.  * Rename the file "fname" into a backup
  125.  * copy. On Unix the backup has the same name as the
  126.  * original file, with a "~" on the end; this seems to
  127.  * be newest of the new-speak. The error handling is
  128.  * all in "file.c". The "unlink" is perhaps not the
  129.  * right thing here; I don't care that much as
  130.  * I don't enable backups myself.
  131.  */
  132. fbackupfile(fname)
  133. char    *fname;
  134. {
  135.     register char    *nname;
  136.  
  137.     if ((nname=malloc(strlen(fname)+1+1)) == NULL)
  138.         return (ABORT);
  139.     (void) strcpy(nname, fname);
  140.     (void) strcat(nname, "~");
  141.     (void) unlink(nname);            /* Ignore errors.    */
  142.  
  143.     /* no rename on System V, so do it dangerous way. */
  144.     if ( link(fname, nname) < 0 || unlink(fname) < 0 ) {
  145.         free(nname);
  146.         return (FALSE);
  147.     }
  148.     free(nname);
  149.     return (TRUE);
  150. }
  151.  
  152. /*
  153.  * The string "fn" is a file name.
  154.  * Perform any required case adjustments. All sustems
  155.  * we deal with so far have case insensitive file systems.
  156.  * We zap everything to lower case. The problem we are trying
  157.  * to solve is getting 2 buffers holding the same file if
  158.  * you visit one of them with the "caps lock" key down.
  159.  * On UNIX file names are dual case, so we leave
  160.  * everything alone.
  161.  */
  162. adjustcase(fn)
  163. register char    *fn;
  164. {
  165. #if    0
  166.     register int    c;
  167.  
  168.     while ((c = *fn) != 0) {
  169.         if (c>='A' && c<='Z')
  170.             *fn = c + 'a' - 'A';
  171.         ++fn;
  172.     }
  173. #endif
  174. }
  175.